/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.jms.JMSException;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.sinodata.bsm.common.vo.CollectField;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;
import com.sinodata.bsm.inmp.cache.CacheFactory;
import com.sinodata.bsm.inmp.cache.CollectFieldCache;
import com.sinodata.bsm.inmp.cache.ResCache;
import com.sinodata.bsm.inmp.cache.ResTypeCache;
import com.sinodata.bsm.inmp.conf.ConfManager;
import com.sinodata.bsm.inmp.conf.RTPerfCache;
import com.sinodata.bsm.inmp.message.Messenger;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-19 上午11:09:17          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class CollectJob implements Job {
    protected final static String JOB_KEY_RESID = "JOB_KEY_RESID";
    protected final static String JOB_KEY_TASKID = "JOB_KEY_TASKID";
    private final ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);
    private final ResTypeCache resTypeCache = (ResTypeCache) CacheFactory.getCache(ResTypeCache.class);
    private CollectFieldCache fieldCache = (CollectFieldCache) CacheFactory.getCache(CollectFieldCache.class);

    private static Logger logger = Logger.getLogger(CollectJob.class);

    /**
     * 
     */
    public CollectJob() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            JobDataMap dataMap = context.getJobDetail().getJobDataMap();
            dataMap.put("begin", System.currentTimeMillis());
            Long resId = dataMap.getLong(JOB_KEY_RESID);
            Long taskId = dataMap.getLong(JOB_KEY_TASKID);
            Boolean temporary = dataMap.getBoolean("IS_TEMPORARY");

            Res res = resCache.get(resId);
            if (res == null || res.getPaused() == 1) {
                return;
            }
            try {
                String perf = RTPerfCache.getInstance().get(res.getInternalId());
                if (perf == null) {
                    return;
                }

                ResPropertyValue valuex = new ResPropertyValue();
                valuex.setResId(res.getId());
                valuex.setTaskId(taskId);
                valuex.setTemporary(temporary);
                valuex.setTime(Calendar.getInstance().getTime());
                List<PropValue> propValueList = new ArrayList<PropValue>();

                Long typeId = resTypeCache.get(res.getResTypeId()).getParentId();

                String typeProps = ConfManager.getInstance().getTypePropsMap().get(typeId);

                String[] props = typeProps.split("[,]");

                String[] values = perf.split("[,]");
                if (values.length != props.length) {
                    return;
                }
                for (int i = 0; i < values.length; i++) {
                    Long propId = Long.parseLong(props[i]);
                    CollectField field = fieldCache.getByPropId(propId);
                    PropValue propValue = ResPropertyValue.createPropValue(propId, values[i], field.getFieldName());
                    propValueList.add(propValue);
                }

                valuex.setValues(propValueList.toArray(new PropValue[propValueList.size()]));
                sendPropertyValue(valuex);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Throwable e) {
            logger.error("failed to collect property value", e);
        }
    }

    /**
     * 发送实时指标值到center的队列中。
     */
    private void sendPropertyValue(ResPropertyValue valuex) {
        logger.info(valuex);
        try {
            Messenger.getInstance().sendPropertyValue(valuex);
        } catch (JMSException e) {
            logger.error("failed to send property value:" + valuex, e);
        }
    }

}
